package cn.tedu.bece.front.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.jwt.JWTUtil;
import cn.tedu.bece.front.common.response.JsonResult;
import cn.tedu.bece.front.pojo.dto.BabyQuery;
import cn.tedu.bece.front.pojo.dto.BabySaveParam;
import cn.tedu.bece.front.pojo.entity.Baby;
import cn.tedu.bece.front.pojo.vo.BabyVO;
import cn.tedu.bece.front.service.BabyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/baby")
public class BabyController {
    @Autowired
    BabyService babyService;

    @PostMapping("/save")
    public JsonResult saveBaby(@RequestBody BabySaveParam babySaveParam){
        log.debug("保存宝宝：babySaveParam={}",babySaveParam);
        babyService.saveBaby(babySaveParam);
        return JsonResult.success();
    }

    @GetMapping("/selectList")
    public JsonResult selectBabyList(BabyQuery babyQuery){
        log.debug("查询宝宝列表：babyQuery={}",babyQuery);
        //从token中解析出userId
        Integer userId = StpUtil.getLoginIdAsInt();
        babyQuery.setUserId(userId);
        List<BabyVO> list = babyService.selectBaby(babyQuery);
        return JsonResult.success(list);
    }

    @GetMapping("/selectById")
    public JsonResult selectBabyById(BabyQuery babyQuery){
        log.debug("通过id查询宝宝：babyQuery={}",babyQuery);
        Integer userId = StpUtil.getLoginIdAsInt();
        babyQuery.setUserId(userId);
        BabyVO babyVO = babyService.selectBabyById(babyQuery);
        return JsonResult.success(babyVO);
    }

}
